package com.cslg.ac.demo.dao.host;

import com.cslg.ac.demo.dao.host.po.HostFosterPO;
import org.apache.ibatis.annotations.*;

@Mapper
public interface HostFosterDAO {
    String TABLE_NAME = "r_host_foster";
    String SELECT_FIELDS = "id, host_id hostId, foster_id fosterId, begin_time beginTime, end_time endTime, foster_send fosterSend, foster_back fosterBack";

    @Insert({"insert into ", TABLE_NAME, "(host_id, foster_id, begin_time, end_time) values(#{hostId}, #{fosterId}, #{beginTime}, #{endTime})"})
    void insert(HostFosterPO hostFosterPO);

    @Update({"update ", TABLE_NAME, "set begin_time = #{beginTime}, end_time = #{endTime}, foster_send = #{fosterSend}, " +
            "foster_back = #{fosterBack}, foster_id = #{fosterId} where id = #{id}"})
    void updateById(HostFosterPO hostFosterPO);

    @Update({"update ", TABLE_NAME, "set begin_time = #{beginTime}, end_time = #{endTime}, foster_send = #{fosterSend}, " +
            "foster_back = #{fosterBack}, finish = #{finish} where foster_id = #{fosterId} and finish != '1'"})
    void updateByFosterId(HostFosterPO hostFosterPO);

    @Select({"select ",SELECT_FIELDS, "from ", TABLE_NAME, "where id = #{id}"})
    HostFosterPO selectById(@Param("id")int id);

    @Select({"select ",SELECT_FIELDS, "from ", TABLE_NAME, "where foster_id = #{fosterId} and finish != '1'"})
    HostFosterPO selectByFosterId(@Param("fosterId")int fosterId);

    @Select({"select a.pet_id from r_user_pet a,r_host_foster c where a.user_id = #{userId} and a.pet_id = c.foster_id" +
            " and c.foster_send = '0' limit #{offset}, #{num}"})
    Integer[] selectFosterNotSend(@Param("userId")int userId, @Param("offset")int offset, @Param("num")int num);

    @Select({"select a.pet_id from r_user_pet a,r_host_foster c where a.user_id = #{userId} and a.pet_id = c.foster_id" +
            " and c.foster_back = '0' limit #{offset}, #{num}"})
    Integer[] selectFosterNotBack(@Param("userId")int userId, @Param("offset")int offset, @Param("num")int num);

    @Delete({"delete from ", TABLE_NAME, "where foster_id = #{fosterId}"})
    void delete(@Param("fosterId")int fosterId);

    @Select({"select distinct a.pet_id from r_user_pet a where  a.user_id = #{userId} and a.pet_id " +
            "not in( select r_host_foster.foster_id from r_host_foster) and a.pet_type = '0' limit #{offset}, #{num}"})
    Integer[] selectFosterNotFoster(@Param("userId")int userId, @Param("offset")int offset, @Param("num")int num);

}
